package com.cool.service.impl.example;

import com.cool.model.common.ResponseCode;
import com.cool.model.common.ServerResponse;
import com.cool.repository.example.TemplateRepository;
import com.cool.service.example.TemplateServcie;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Title: TemplateServcieImpl
 * Description: 模板服务实现层
 *
 * @author yls
 * @created 2018/11/30 14:45
 */
@Service
public class TemplateServcieImpl implements TemplateServcie {

    @Autowired
    private TemplateRepository templateRepository;

    @Override
    public ServerResponse detail(Long id) {
        //根据角色所属id，查询角色信息
        Specification<AdminDictionary> specification = Specifications.<AdminDictionary>and()
                .eq("id", id)
               // .eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        Optional<AdminDictionary> optional = templateRepository.findOne(specification);
        //角色信息不存在
        if (!optional.isPresent()) {
            return ServerResponse.createByErrorMessage(ResponseCode.MESSAGE_NOT_EXIST.getCode(), ResponseCode.MESSAGE_NOT_EXIST.getDesc());
        }
        return ServerResponse.createBySuccess(optional.get());
    }

    @Override
    public ServerResponse list() {
        // 方案一 、添加按创建时间降序查询
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        //未删除
        Specification<AdminDictionary> specification = Specifications.<AdminDictionary>and()
                //.eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        List<AdminDictionary> list = templateRepository.findAll(specification, sort);
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse pageList(Integer pageNumber, Integer pageSize, HttpServletRequest request) {
        //未删除
        Specification<AdminDictionary> specification = Specifications.<AdminDictionary>and()
               // .eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        Page<AdminDictionary> pageList = templateRepository.findAll(specification, PageRequest.of(pageNumber - 1, pageSize, Sort.Direction.DESC, "createDate"));
        return ServerResponse.createBySuccess(pageList);
    }

    @Override
    public ServerResponse save(AdminDictionary adminDictionary, HttpServletRequest request) {
            //创建更新时间
            adminDictionary.setCreateDate(new Date());
            adminDictionary.setModifyDate(new Date());
            templateRepository.save(adminDictionary);
            return ServerResponse.createBySuccess(ResponseCode.SAVE_SUCCESS.getCode(),ResponseCode.SAVE_SUCCESS.getDesc());
    }

    @Override
    public ServerResponse update(AdminDictionary adminDictionary, HttpServletRequest request) {
        if(adminDictionary.getId()==null){
            return ServerResponse.createByError();
        }
        //创建更新时间
        adminDictionary.setCreateDate(new Date());
        adminDictionary.setModifyDate(new Date());
        templateRepository.save(adminDictionary);
        return ServerResponse.createBySuccess(ResponseCode.UPDATE_SUCCESS.getCode(),ResponseCode.UPDATE_SUCCESS.getDesc());
    }

    @Override
    public ServerResponse deleteById(Long id) {
        //根据角色所属id，查询角色信息
        Specification<AdminDictionary> specification = Specifications.<AdminDictionary>and()
                .eq("id", id)
                .build();
        Optional<AdminDictionary> optional = templateRepository.findOne(specification);
        //角色信息不存在
        if (!optional.isPresent()) {
            return ServerResponse.createByErrorMessage(ResponseCode.MESSAGE_NOT_EXIST.getCode(), ResponseCode.MESSAGE_NOT_EXIST.getDesc());
        }
        templateRepository.deleteById(id);
        return ServerResponse.createBySuccess(ResponseCode.DELETE_SUCCESS.getCode(),ResponseCode.DELETE_SUCCESS.getDesc());
    }
}
